package cn.sundat.web.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;

import cn.sundat.model.Category;
import cn.sundat.model.News;
import cn.sundat.model.PageBean;
import cn.sundat.service.NewsService;
import cn.sundat.utils.FastJsonUtil;

public class NewsAction extends ActionSupport implements ModelDriven<News>{

	private News news = new News();
	public News getModel() {
		return news;
	}
	@Resource(name="newsService")
	private NewsService newsService;
	
	//属性驱动封装当前页
	private Integer currPage=1;
	public void setCurrPage(Integer currPage) {
		if(currPage==null) {
			currPage=1;
		}
		this.currPage = currPage;
	}
	
	//属性驱动封装页面数量
	private Integer pageSize=10;
	
	public void setPageSize(Integer pageSize) {
		if(pageSize==null) {
			pageSize=8;
		}
		this.pageSize = pageSize;
	}
	
	/**
	 * 根据id查询动态新闻
	 * @return
	 */
	public String findById() {
		news = newsService.findById(news.getNews_id());
		ValueStack vs = ActionContext.getContext().getValueStack();
		ServletActionContext.getRequest().setAttribute("dt", news);
		return "detail";
	}
	
	/**
	 * 分页加载新闻
	 * @return
	 */
	public String findByPage() {
		long categoryId = Long.parseLong(ServletActionContext.getRequest().getParameter("categoryId"));
		DetachedCriteria criteria = DetachedCriteria.forClass(News.class);
		Category c = new Category();
		c.setCate_id(categoryId);
		criteria.add(Restrictions.eq("category", c));
		criteria.add(Restrictions.eq("news_isshow", 1));
		criteria.addOrder(Order.desc("news_id"));
		PageBean<News> pbList = newsService.findByPage(currPage, pageSize, criteria);
		ServletActionContext.getRequest().setAttribute("pbList", pbList);
		return "list";
	}
	
	/**
	 * 手机资料下载功能,默认只支持100条
	 * @return
	 */
	public String findByPageOnMbile() {
		long categoryId = Long.parseLong(ServletActionContext.getRequest().getParameter("categoryId"));
		DetachedCriteria criteria = DetachedCriteria.forClass(News.class);
		Category c = new Category();
		c.setCate_id(categoryId);
		criteria.add(Restrictions.eq("category", c));
		criteria.add(Restrictions.eq("news_isshow", 1));
		criteria.addOrder(Order.desc("news_id"));
		PageBean<News> pbList = newsService.findByPage(currPage, 100, criteria);
		ServletActionContext.getRequest().setAttribute("pbList", pbList);
		return "shoujilist";
	}
	
	public String findByAjax() {
		long categoryId = Long.parseLong(ServletActionContext.getRequest().getParameter("categoryId"));
		DetachedCriteria criteria = DetachedCriteria.forClass(News.class);
		Category c = new Category();
		c.setCate_id(categoryId);
		criteria.add(Restrictions.eq("category", c));
		PageBean<News> pbList = newsService.findByPage(currPage, pageSize, criteria);
		HttpServletResponse response = ServletActionContext.getResponse();
		String jsonString = FastJsonUtil.toJSONString(pbList);
		FastJsonUtil.write_json(response, jsonString);
		return NONE;
	}
	
	/**
	 * 分页查找查找会员风采
	 * @return
	 */
	public String findVipNews() {
		Category c = new Category();
		c.setCate_id(12L);
		Integer pageSize = 10;
		DetachedCriteria criteria = DetachedCriteria.forClass(News.class);
		criteria.add(Restrictions.eq("category", c));
		PageBean<News> pb =  newsService.findVipNews(currPage,pageSize,criteria);
		ServletActionContext.getRequest().setAttribute("npb", pb);
		return "huiyuan";
	}
	
}